package com.ldzy.dao;
import com.github.pagehelper.Page;
import com.ldzy.domain.Record;
import com.ldzy.domain.Tool;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface ToolDao {
    @Select("SELECT * FROM tool where status !=3  order by uploadtime DESC")
    @Results(id = "bookMap",value = {
            @Result(id =true,column = "id",property ="id"),
            @Result(column = "name",property = "name"),
            @Result(column = "pagination",property = "pagination"),
            @Result(column = "price",property = "price"),
            @Result(column = "uploadtime",property = "uploadtime"),
            @Result(column = "status",property = "status"),
            @Result(column = "borrower",property = "borrower"),
            @Result(column = "borrowtime",property = "borrowtime"),
            @Result(column = "returntime",property = "returntime"),
    })

    Page<Tool> selectNewBooks();
//    @Select("select * from tool where id=#{id}")
//    @ResultMap("bookMap")
   Tool findById(String id);
    Integer editTool(Tool tool);

    @Update("<script>" +
            "update tool " +
            "<set>" +
            "           <if test=\"name != null\" >" +
            "                name = #{name}," +
            "            </if>" +
            "            <if test=\"isbn != null\" >" +
            "                isbn = #{isbn}," +
            "            </if>" +
            "            <if test=\"press != null\" >" +
            "                press = #{press}," +
            "            </if>" +
            "            <if test=\"author != null\" >" +
            "                author = #{author}," +
            "            </if>" +
            "            <if test=\"pagination != null\" >" +
            "                pagination = #{pagination}," +
            "            </if>" +
            "            <if test=\"price != null\" >" +
            "                price = #{price}," +
            "            </if>" +
            "            <if test=\"uploadTime != null\" >" +
            "                uploadtime = #{uploadtime}," +
            "            </if>" +
            "            <if test=\"status != null\" >" +
            "                status = #{status}," +
            "            </if>" +
            "            <if test=\"borrower != null\" >" +
            "                borrower= #{borrower }," +
            "            </if>" +
            "            <if test=\"borrowTime != null\" >" +
            "                borrowtime = #{borrowtime}," +
            "            </if>" +
            "            <if test=\"returnTime != null\" >" +
            "                returntime = #{returntime}" +
            "            </if>"+
            "</set>" +
            "where id=#{id}" +
            "</script>")
    public int edit(Tool tool);

    @Select("<script>" +
            "select * from tool " +
            "<where>" +
            "<if test=\"id != null\"> AND id=#{id} </if>" +
            "<if test=\"status != null\"> AND status=#{status} </if>" +
            "<if test=\"name != null\"> AND  name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test=\"uploadtime != null\"> AND uploadtime like  CONCAT('%', #{uploadtime},'%') </if>" +
            "<if test=\"pagination != null\"> AND pagination like  CONCAT('%', #{pagination},'%')</if>" +
            "</where>" +
            "</script>")
    public Page<Tool> findByBookWithUpLoadTimeDesc(Tool tool);//如果book为空，查询所有，如果book.name="abc",查询书名含有abc的书


    @Update("update tool set status = #{status},borrower= #{borrower },borrowtime = #{borrowtime}," +
            "returntime = #{returntime} where id=#{id}")
    public int returnConfirm(Tool tool);

    @Select("select * from tool where name = #{name}")
    List<Tool> findByName(String name);

    @Select("select * from tool where name = #{name}")
    List<Tool> findbyName(String name);

    @Select("<script>" +
            "select * from record " +
            "<where>" +
            "<if test=\"id != null\"> AND id=#{id} </if>" +
            "<if test=\"toolname != null\"> AND toolname=#{status} </if>" +
            "<if test=\"borrower != null\"> AND  borrower  like  CONCAT('%',#{borrower},'%')</if>" +
            "<if test=\"borrowtime != null\"> AND borrowtime like  CONCAT('%', #{borrowtime},'%') </if>" +
            "<if test=\"remandtime != null\"> AND remandtime like  CONCAT('%', #{remandtime},'%')</if>" +
            "</where>" +
            "</script>")
    public Page<Record> findByBook(Record tool);//如果book为空，查询所有，如果book.name="abc",查询书名含有abc的书



}
